import {
	useI18n
} from "@/locales/setupI18n";

/**
 * 审核状态
 * @param status 状态码
 * @param msg 状态消息
 * @param errorMessageMode 错误消息提示模式: 'none' | 'modal' | 'message'
 */
export function checkStatus(status, msg, errorMessageMode = 'message') {
	const {
		t
	} = useI18n();
	let statusMsg = '';
	let errMessage = '';
	const msgString = msg !== undefined && msg !== null && msg !== '' ? ` ${msg}` : '';
	if (!status) {
		errMessage = msgString;
	}
	switch (status) {
		case 0:
			statusMsg = `${status} - ${t('bluetooth.msg0')}${msgString}`;
			break;
		case 10000:
			statusMsg = `${status} - ${t('bluetooth.msg10000')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10001:
			statusMsg = `${status} - ${t('bluetooth.msg10001')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10002:
			statusMsg = `${status} - ${t('bluetooth.msg10002')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10003:
			statusMsg = `${status} - ${t('bluetooth.msg10003')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10004:
			statusMsg = `${status} - ${t('bluetooth.msg10004')}${msgString}`;
			errMessage = statusMsg;
			// 提示未找到指定的服务，跳转去打印设置页
			const pages = getCurrentPages();
			let currentRoute = '';
			if (pages && pages.length > 0) {
				const currentPage = pages[pages.length - 1];
				if (currentPage.route) {
					currentRoute = currentPage.route;
				}
			}
			// console.log(currentPage);
			// uni.showModal({
			// 	title: '蓝牙10004',
			// 	content: currentRoute
			// });
			if (currentRoute !== 'pages/sys/profile/printerSetup') {
				// 拦截路由，并且跳转去打印设置页
				uni.navigateTo({
					url: '/pages/sys/profile/printerSetup',
					passedParams: {
						info: '未找到指定的服务',
					},
				});
			}
			break;
		case 10005:
			statusMsg = `${status} - ${t('bluetooth.msg10005')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10006:
			statusMsg = `${status} - ${t('bluetooth.msg10006')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10007:
			statusMsg = `${status} - ${t('bluetooth.msg10007')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10008:
			statusMsg = `${status} - ${t('bluetooth.msg10008')}${msgString}`;
			errMessage = statusMsg;
			break;
		case 10009:
			statusMsg = `${status} - ${t('bluetooth.msg10009')}${msgString}`;
			errMessage = statusMsg;
			break;
		default:
			if (status !== undefined && status !== null && status !== 'undefined' && status !== 'null') {
				statusMsg = `${status} - ${t('bluetooth.errorMessage')}${msgString}`;
			} else {
				statusMsg = `${t('bluetooth.errorMessage')}${msgString}`;
			}
			errMessage = statusMsg;
			break;
	}
	// console.log('checkStatus', { status, msg, errorMessageMode, statusMsg, errMessage });
	if (errMessage) {
		// console.log('checkStatus', errorMessageMode);
		if (errorMessageMode === 'modal') {
			uni.showModal({
				title: t('bluetooth.errorTip'),
				content: errMessage,
				showCancel: false
			});
		} else if (errorMessageMode === 'message') {
			uni.showToast({
				title: errMessage,
				// duration: 2000,
				icon: 'none'
			});
		}
	}
}
